#!/bin/bash

DELAY=2
CONFIG_PATH="/etc/wpa_supplicant/"
SETCOLOR_SUCCESS="echo -en \\033[1;32m"
SETCOLOR_NORMAL="echo -en \\033[0;39m"
SETCOLOR_FAILURE="echo -en \\033[1;31m"
SETCOLOR_SEPARATOR="echo -en \\033[1;34m"
SETCOLOR_MENU="echo -en \\033[1;36m"

print_wifi_ssid() {
  clear
  for ((i=0; i < ${#WIFI_SSID_ARRAY[@]}; i++)) do
    echo -e "\n ${i} ${WIFI_SSID_ARRAY[$i]}"
  done
}


# get array wifi ssid (configs)
WIFI_SSID_ARRAY=$(ls -w 1 "$CONFIG_PATH" | grep -oi ".*[^.conf$]" | fmt -w 2500)
WIFI_SSID_ARRAY=($WIFI_SSID_ARRAY)

# print configs and selection will be deleted config file
while true; do
  echo -e $(print_wifi_ssid)
  read -p "Select file for edit: " SELECT_CONFIG_FILE

  if [[ "$SELECT_CONFIG_FILE" == [0-9] ]]; then
     CONFIG_FILE=${WIFI_SSID_ARRAY["$SELECT_CONFIG_FILE"]}
   else
     $SETCOLOR_FAILURE
     echo "This wifi ssid is not =("
     $SETCOLOR_NORMAL
     sleep $DELAY
     continue
   fi

   if [[ "$CONFIG_FILE" == "" ]]; then
      $SETCOLOR_FAILURE
      echo "This wifi ssid is not =("
      $SETCOLOR_NORMAL
      sleep $DELAY
      continue
   fi

   break
done

# Open file in vim
CONFIG_FILE_PATH="$CONFIG_PATH${CONFIG_FILE}.conf"
sudo vim $CONFIG_FILE_PATH

